--[[
   @brief    main.common
   @author   xiaokunyang
   @version  1.0
   @date     2015-03-13
--]]

ierd_tgp.tpf_ui.Log("loading main.common.lua");

QosMainBtnId = {
    kPauseDownload = 53,  -- Banner暂停下载按钮点击
    kContinueDownload = 54,  -- Banner继续下载按钮点击
    kCancelUpdate = 55, -- Banner取消更新按钮点击
    kUpdateGame = 56,   -- Banner更新游戏
};

MainCommon = {
    color = {green = "RGB:0,255,0", yellow = "RGB:255,255,0", red = "RGB:255,0,0"};
    color_mode = {
        kColorAdapter = 0,
        kLight = 1,  -- 亮色模式
        kDark = 2,  -- 暗色模式
    };
    -- 是否使用native
    use_navtive = function(native)
        if native == nil then
            return false;
        end

        return native;
    end;
};

function MainCommon.get_element(uilib, parent_id, name_path, interface_type)
    MainCommon.log(string.format("get_element, %s(%s)", name_path, interface_type))
    if uilib == nil then
        MainCommon.log(string.format("get_element FAIL, uilib is NIL, %s(%s)",
            name_path, interface_type), true);
        return nil;
    end
    local path_str,path_hoder = U8toCUIStr(name_path);
    --self:log("call GetControlIDbyPath");
    local id = uilib:GetControlIDbyPath(path_str, parent_id);
    if id == 0 then
        MainCommon.log(string.format("get_element FAIL, id == 0, %s(%s)",
            name_path, interface_type));
        return nil;
    end;
        
    local pControlSpecial = MainCommon.get_ctrl_ptr(uilib, id, interface_type);
    if pControlSpecial == nil then
        MainCommon.log(string.format("get_element FAIL, pStatic == nil, %s(%s)",
            name_path, interface_type))
        return nil;
    end;
    
    return pControlSpecial;
end;

function MainCommon.get_child_id(uilib, parent_id, name_path)
    if uilib == nil then
        MainCommon.log(string.format("get_element FAIL, uilib is NIL, parent_id:%d, path:%s",
            parent_id, name_path), true);
        return Tenio.INVALID_CONTROLID;
    end
    local path_str,path_hoder = U8toCUIStr(name_path);
    local id = uilib:GetControlIDbyPath(path_str, parent_id);

    return id;
end;

function MainCommon.get_child_ptr(uilib, parent_id, name_path)
    if uilib == nil then
        MainCommon.log(string.format("get_element FAIL, uilib is NIL, parent_id:%d, path:%s",
            parent_id, name_path), true);
        return Tenio.INVALID_CONTROLID;
    end
    local path_str,path_hoder = U8toCUIStr(name_path);
    local id = uilib:GetControlIDbyPath(path_str, parent_id);

    local pControl = uilib:GetControlPtr(id);
    return pControl;
end;

function MainCommon.log(msg, core_msg)
    base_utility.log(string.format("[MainCommon] %s", msg), core_msg);
end

function MainCommon.get_ctrl_ptr(uilib, id, interface_type)
    --self:log(string.format("got id:%d", id));
    --self:log("call GetControlPtr");
    local pControl = uilib:GetControlPtr(id);
    if pControl == nil then
        MainCommon.log(string.format("get_ctrl_ptr FAIL, pControl == nil, id:%d, (%s)",
            id, interface_type))
        return nil;
    end;
    
    --self:log("call tolua.cast");
    local pControlSpecial = ui_utility.get_ctrl_ptr_by_id(id);
    if pControlSpecial == nil then
        MainCommon.log(string.format("get_ctrl_ptr FAIL, pControlSpecial == nil, %d, (%s)",
           id, interface_type))
        return nil;
    end;
    
    return pControlSpecial;
end

function MainCommon.get_parent_id(uilib, id)
    --self:log(string.format("got id:%d", id));
    --self:log("call GetControlPtr");
    local pControl = uilib:GetControlPtr(id);
    if pControl == nil then
        MainCommon.log(string.format("get_parent_id FAIL, pControl is NIL, id:%d",
           id));
        return Tenio.INVALID_CONTROLID;
    end;
    
    local parent = pControl:GetParent();
    if parent == nil then
        MainCommon.log(string.format("get_parent_id FAIL, parent is NIL, %d",
            id))
        return Tenio.INVALID_CONTROLID;
    end;
    
    return parent:GetControlID();
end

function MainCommon.set_static_text(uilib, parent_id, child_path, text_str)
    local pStatic = MainCommon.get_element(uilib, parent_id, child_path, "Tenio::ITStatic");
    if pStatic ~= nil then
        MainCommon.log(string.format("[set_static_text]set text, child_path:%s, text:%s",
           child_path, text_str));
        
        local str,str_hoder = U8toCUIStr(text_str);
        
        pStatic:SetText(str);
        return true;
    else
        MainCommon.log(string.format("[set_static_text]pStatic is NIL, child_path:%s, text:%s",
           child_path, text_str), true);
        return false;
    end
end

function MainCommon.set_static_text_ex(parent_id, child_path, text_str)
    return MainCommon.set_static_text(ui_utility.uilib(), parent_id, child_path, text_str);
end

function MainCommon.set_button_caption(uilib, parent_id, child_path, text_str)
    local button = MainCommon.get_element(uilib, parent_id, child_path, "Tenio::ITButton");
    if button ~= nil then
        MainCommon.log(string.format("[set_button_caption]set text, child_path:%s, text:%s",
           child_path, text_str));
        
        local str,str_hoder = U8toCUIStr(text_str);
        
        button:SetCaption(str);
        return true;
    else
        MainCommon.log(string.format("[set_button_caption]button is NIL, child_path:%s, text:%s",
           child_path, text_str), true);
        return false;
    end
end

function MainCommon.SetButtonTextColor(uilib, parent_id, child_path, text_color)
    local button = MainCommon.get_element(uilib, parent_id, child_path, "Tenio::ITButton");
    if button ~= nil then       
        local btn_id = button:GetControlID();
        ui_utility.setproperty_by_id(btn_id, "TextColor", text_color);
        ui_utility.setproperty_by_id(btn_id, "TextColorHover", text_color);
        ui_utility.setproperty_by_id(btn_id, "TextColorPressed", text_color);
        return true;
    else
        return false;
    end
end

function MainCommon.set_visible(uilib, id, visible)

    MainCommon.log(string.format("set_visible, id:%d, visible:%s",
       id, tostring(visible)));
    local control = ui_utility.get_ctrl_ptr_by_id(id);
    if control then
        if visible then
            control:Show(TRUE);
        else
            control:Show(FALSE);
        end
    else
        base_utility.log("[MainCommon.set_visible]control is NIL");
    end
end

function MainCommon.set_enabled(uilib, parent_id, child_path, enabled)  
    MainCommon.log(string.format("set_enabled, child_path:%s, enabled:%s",
        tostring(child_path), tostring(enabled)));
    if nil == parent_id or nil == child_path then
        MainCommon.log(string.format("[set_enabled]param invalid, child_path:%s, enabled:%s",
            tostring(child_path), tostring(enabled)), true);
        return false;
    end

    local pControl = MainCommon.get_child_ptr(uilib, parent_id, child_path);
    if pControl ~= nil then

        if enabled then
            pControl:Enable(TRUE);
        else
            pControl:Enable(FALSE);
        end
        return true;
    else
        MainCommon.log(string.format("[set_enabled]button is NIL, child_path:%s, enabled:%s",
           child_path, tostring(enabled)), true);
        return false;
    end
end

function MainCommon.set_level_progress(uilib, parent_id, child_path, progress)
    local level = MainCommon.get_element(uilib, parent_id, child_path, "Tenio::ITLevel");
    if level ~= nil then
        if progress == nil then
            MainCommon.log(string.format("[set_level_progress]progress is NIL, child_path:%s",
               child_path), true);
            return;
        end
        
        MainCommon.log(string.format("[MainCommon]set_level_progress, child_path:%s, progress:%d",
           child_path, progress));
        
        level:SetPos(progress, TRUE);
        return true;
    else
        MainCommon.log(string.format("[set_level_progress]level is NIL, child_path:%s",
           child_path), true);
        return false;
    end
end

function MainCommon.set_property(uilib, parent_id, child_path, pro_name, pro_value)
    MainCommon.log(string.format("set_property," ..
        " parent_id:%d, child_path:%s, pro_name:%s, pro_value:%s",
       parent_id, child_path, pro_name, pro_value));

    local child_id = MainCommon.get_child_id(uilib, parent_id, child_path);
    if child_id == Tenio.INVALID_CONTROLID then
        MainCommon.log(string.format("[set_property]child_id is INVALID_CONTROLID," ..
            " child_path:%s, pro_name:%s, pro_value:%s",
           child_path, pro_name, pro_value), true);
        return false;
    end

    return ui_utility.setproperty_by_id(child_id, pro_name, pro_value);
end


function MainCommon.set_customdata(uilib, parent_id, name_path, custom_data)
    local pControl = MainCommon.get_child_ptr(uilib, parent_id, name_path);
    if pControl == nil then
        MainCommon.log(string.format("set_customdata, pControl is NIL, parent:%d, child:%s",
           parent_id, name_path));
        return false;
    end;
    
    pControl:SetCustomData(custom_data);
    
    return true;
end

function MainCommon.get_customdata(uilib, parent_id, name_path)
    local pControl = MainCommon.get_child_ptr(uilib, parent_id, name_path);
    if pControl == nil then
        MainCommon.log(string.format("get_customdata, pControl is NIL, parent:%d, child:%s",
           parent_id, name_path));
        return 0;
    end;
    
    return pControl:GetCustomData();
end

function MainCommon.get_ctrl_customdata(uilib, id)
    local pControl = MainCommon.get_ctrl_ptr(uilib, id, "");
    if pControl == nil then
        MainCommon.log(string.format("get_ctrl_customdata, get_ctrl_ptr returned NIL, id:%d",
                                    id), true);
        return 0;
    end
    
    return pControl:GetCustomData();
end

function MainCommon.show_child(uilib, parent_id, name_path, show)
    MainCommon.log(string.format("show_child, parent:%d, child:%s, show:%s",
       parent_id, name_path, tostring(show)));
    local pControl = MainCommon.get_child_ptr(uilib, parent_id, name_path);
    if pControl == nil or pControl == NULL then
        MainCommon.log(string.format("show_child, pControl is NIL, parent:%d, child:%s",
           parent_id, name_path), true);
        return false;
    end;
    
    if show then
        pControl:Show(TRUE);
    else
        pControl:Show(FALSE);
    end
    
    return true;
end

-- 只显示name_toshow指定的一个孩子，其余孩子不显示，如果没有指定或指定的孩子不存在，则全部不显示
function MainCommon.show_only_child(uilib, parent_id, name_toshow)
    MainCommon.log(string.format("[show_only_child]show_only_child, parent_id:%d, name_toshow:%s",
       parent_id, tostring(name_toshow)));
    local MAX_NAME = 260;
    local ui_str, mem_holder = NewUIStr(MAX_NAME);
    local parent = uilib:GetControlPtr(parent_id);
    if parent == nil then
        MainCommon.log(string.format("[MainCommon.show_only_child]parent is NIL, parent_id:%d," ..
            " name_toshow:%s",
           parent_id, tostring(name_toshow)));
        return false;
    end
    local any_show = false;
    local next_child = parent:GetFirstChild();
    while next_child ~= nil do
        next_child:GetControlName(ui_str, MAX_NAME);
        local name = UIStrtoU8(ui_str);
        if name == name_toshow then
            MainCommon.log(string.format("[show_only_child]found it, show it"));
            next_child:Show(TRUE);
            any_show = true;
        else
            next_child:Show(FALSE);
        end
        
        -- 上层兄弟就是下一个子控件
        next_child = next_child:GetUpperSibling();
    end
    
    return any_show;
end

function MainCommon.show_only_child_ex(parent_id, name_toshow)
    return MainCommon.show_only_child(ui_utility.uilib(), parent_id, name_toshow);
end

function MainCommon.json_to_table(json_str)
    if json_str == nil then
        return nil;
    else
        return base_utility.json_to_table(json_str);
    end
end

function MainCommon.json2game_id(json_str)
    local param = MainCommon.json_to_table(json_str);
    local game_id = 0;
    if param ~= nil and param.game_id ~= nil then
        game_id = param.game_id;
    else
        MainCommon.log("[MainCommon]json2game_id, param is NIL");
    end
    
    return game_id;
end

function MainCommon.format_speed(download_speed)
    local speed_str = "";
    local unit = "";
    if (download_speed >= 1000*1024) then
        local speed = download_speed / (1024 * 1024);
        speed_str = string.format("%.2f M/s", speed);
    else
        local speed = download_speed/1024;
        speed_str = string.format("%d KB/s", speed);
    end
    return speed_str;
end


function MainCommon.format_time(remain_time)
    local time = "";
    if (remain_time == 0 or remain_time >= 100 * 3600) then
        time = "--:--:--";
    else
        local h = remain_time/3600;
        
        local m = remain_time%3600/60;
        
        local s = remain_time%60;
        
        time = string.format("%02d:%02d:%02d", h, m , s);
    end
    
    return time;
end

function  MainCommon.format_pos(pos)  
    if nil == pos then
        return "(nil)";
    end

    return "(" .. tostring(pos.x) .. "," ..  tostring(pos.y) .. ")"; 
end
   
function  MainCommon.format_size(size)
    if nil == size then
        return "[nil]";
    end

    return "[" .. tostring(size.width) .. "," ..  tostring(size.height) .. "]"; 
end

function MainCommon.arrage_ctrl_h(uilib, parent_id, base_offset, space, ctr_name_list, show_list)
    MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]base_offset: %d" ..
                                " (space:%d)", base_offset, space));
    if ctr_name_list == nil or show_list == nil then
        MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]ctr_name_list or show_list is NIL"),
                                      true);
        return;
    end
    
    local count = table.getn(ctr_name_list);
    if count == 1 then
        MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]count == 1"), true);
        return;
    end
    
    local offset_x = base_offset;
    for i=1, count do
        local ctrl_name = ctr_name_list[i];
        if ctrl_name then
            -- 取控件
            local ctrl = MainCommon.get_child_ptr(uilib, parent_id, ctrl_name)
            -- 如果可见，设置便宜
            if ctrl then
                local show = show_list[ctrl_name];
                if show then
                    local rect = ctrl:GetRectEx(FALSE,TRUE);
                    local new_pos = {x=offset_x, y=rect.top};
                    rect = MainCommon.move_rect(rect, new_pos);
                    MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]move %s to (%s)",
                                                ctrl_name, MainCommon.rect_to_string(rect)));
                    ctrl:MoveEx(rect,TRUE);
                    -- 确定下一个坐标
                    offset_x = rect.right + space;
                    MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]next offset_x: %d" ..
                                                " (space:%d)", offset_x, space));
                else
                    MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]not visible, name:%s",
                                              ctrl_name));
                end
            else
                MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]ctrl is NIL, name:%s",
                                             ctrl_name), true);
            end
        else
            MainCommon.log(string.format("[MainCommon.arrage_ctrl_h]ctrl_name is NIL"), true);
        end
    end
end

function MainCommon.get_rect(uilib, parent_id, name_path)
    local ptr = MainCommon.get_child_ptr(uilib,parent_id,name_path);
    if ptr == nil or ptr == NULL then
        return nil;
    end
    
    local rect = ptr:GetRectEx(FALSE,TRUE);
    return rect;
end

function MainCommon.get_ctrl_rect_string(id)
    local ptr = ui_utility.get_ctrl_ptr_by_id(id);
    if ptr then
        return MainCommon.rect_to_string(ptr:GetRect(FALSE),TRUE);
    end

    return "NULL";
end

function MainCommon.move_rect(rect, new_pos)
    MainCommon.log(string.format("[MainCommon]move_rect (%s) to new pos:(%d,%d)",
                                MainCommon.rect_to_string(rect), new_pos.x, new_pos.y));
    local width = rect.right - rect.left;
    local height = rect.bottom - rect.top;
    rect.left = new_pos.x;
    rect.top = new_pos.y;
    
    rect.right = rect.left + width;
    rect.bottom = rect.top + height;
    
    return rect;
end

function MainCommon.move_ctrl(parent_id, name_path, to_left, to_top)
    local ctrl = ui_utility.get_ctrl_ptr_by_path(parent_id, name_path);
    if ctrl == nil then
        return;
    end
    local old_rect = ctrl:GetRectEx(FALSE,TRUE);
    local new_pos = {x = to_left, y = to_top };
    local new_rect = MainCommon.move_rect(old_rect, new_pos);
    
    ctrl:MoveEx(new_rect,TRUE);
end

function MainCommon.rect_to_string(rect)
    local str = string.format("%d,%d; %d,%d", rect.left, rect.top, rect.right, rect.bottom);
    return str;
end

function MainCommon.string_empty(str)
    if str == nil then
        return true;
    end
    
    local matched = string.match(str, "^%s*$");
    if matched ~= nil then
        return true;
    end
    
    return false;
end

function MainCommon.clone_table(tbl)
    local json_str = base_utility.table_to_json(tbl);
    local new_tbl = base_utility.json_to_table(json_str);
    return new_tbl;
end

function MainCommon.ReportQosMainBtnClick(btn_id, game_id, run_mode)
      --tqos上报
    qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
    qosparam.oc_type = 1;
    qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kMainBtnClickReport; --按钮上报的点击qosid
    qosparam.int_count = 5;
    qosparam.str_count =1;
    qosparam.int_val[0]= 0;
    qosparam.int_val[1]= btn_id;
    qosparam.int_val[2]= 1;
    qosparam.int_val[3]= game_id;
    qosparam.int_val[4]= run_mode;
    qosparam.str_val1 = tostring(game_id);
    base_utility.lua_qos(qosparam);
    qosparam:delete();
end